﻿using AutoMapper;
using B.S.DataBase.Domain;
using B.S.DataBase.ErrorCode;
using B.S.DataBase.Infrastructure;
using B.S.DataBase.Write.API.Application.Commands;
using MediatR;

namespace B.S.DataBase.Write.API.Application.Handlers
{
    public class CreateImmuneHandler : IRequestHandler<CreateImmuneCommand, APIResult<int>>
    {
        private readonly IBaseRepository<Immune> immuneRepository;
        private readonly IMapper mapper;

        public CreateImmuneHandler(IBaseRepository<Immune> immuneRepository, IMapper mapper)
        {
            this.immuneRepository = immuneRepository;
            this.mapper = mapper;
        }

        public async Task<APIResult<int>> Handle(CreateImmuneCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();

            var entity = mapper.Map<Immune>(request);

            var dto = immuneRepository.AddAsync(entity).Result;

            if (dto > 0)
            {
                result.Code = APIEnums.Success;
                result.Meg = "新增成功";
                result.Data = dto;
            }
            else
            {
                result.Code = APIEnums.Fail;
                result.Meg = "新增失败";
                return await Task.FromResult(result);
            }


            return await Task.FromResult(result);
        }
    }
}
